<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
if (eregi(".inc.php",$HTTP_SERVER_VARS['PHP_SELF']) || eregi(".inc.php",$_SERVER['PHP_SELF'])) {
	echo "<html>\r\n<head>\r\n<title>Forbidden 403</title>\r\n</head>\r\n<body><h3>Forbidden 403</h3>\r\nThe document you are requesting is forbidden.\r\n</body>\r\n</html>";
	exit;
}



// on each page view
// get stored dates and see if it is time
// to check for a reciprocal link


$getLinkData = mysql_query("SELECT * FROM affiliSt_links");
$linkData = mysql_fetch_assoc($getLinkData);

// timestamps
$timeNow = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$timeNextMonth =  mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));

do {
	
	if ($linkData['nextcheck'] < $timeNow && $linkData['nocheck'] == 0 && $linkData['approved'] == 1) {
	
	// check for recip link
	$findlink = @ implode('', file($linkData['reciprocal']));
		if ($findlink) {
		
		$getBacklink = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'adLinkURL'");
		$userBacklink = mysql_fetch_assoc($getBacklink);
		$sitelink = $userBacklink['value'];
		
				if (eregi("$sitelink", $findlink)) {
				$recipConfirm = 1;
				} else {
				$recipConfirm = 0;
				}
			
		} else {
		$recipConfirm = 0;
		}
	
	
	// update the link and set new check timestamp
	$sql = sprintf("UPDATE affiliSt_links SET confirmed = %d, nextcheck = %d WHERE id = %s",
			   quote_smart($recipConfirm),
			   quote_smart($timeNextMonth),
			   quote_smart($linkData['id']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
	
	
		// email if no recip found
		if ($recipConfirm == 0) {
			// get config details
			$getMyEmail = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'adEmail'");
			$myEmail = mysql_fetch_assoc($getMyEmail);
			$getEmailRecip = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'emailNoRecip'");
			$emailRecip = mysql_fetch_assoc($getEmailRecip);
			$getInstallDir = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'installDir'");
			$installDir = mysql_fetch_assoc($getInstallDir);
			$getEmailSig = mysql_query("SELECT * FROM affiliSt_config WHERE name = 'emailSig'");
			$emailSig = mysql_fetch_assoc($getEmailSig);
			
			// reset message
			$messagea = '';
				
			// recip email
			$myemailaddress = $myEmail['value'];
			$emailaddress = $linkData['email'];
			$subject = "Reciprocal Link Not Found";
			if ($linkData['category'] != 0) {
				$linkLink = $installDir['value']."index.php?case=links&ld=1&cat=".$linkData['category'];
			} else {
				$linkLink = $installDir['value']."index.php?case=links&ld=1";
			}
			$emailmessage = $emailRecip['value'];
			$emailfrom = $myemailaddress;
			$messagea .= "Dear ".$linkData['name']."\n\n".$emailmessage."\n".$linkLink."\n\n".$emailSig['value']."\n\n";
			$runtime = (date("d M Y H:i"));
			$messagea .= "Time of the message: $runtime (server time zone)\n\n";
			mail($emailaddress, $subject, $messagea, "From: '$emailfrom'");
		}
	
	
	
	}

} while ($linkData = mysql_fetch_assoc($getLinkData));

?>